﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using NetJsWire.JsonRpcHandler;
using NetJsWire.JsonRpcHandler.Attributes;
using NetJsWire.Widgets.Core;
using NetJsWire.Widgets.Renderers.Core;

namespace NetJsWire.Widgets.Renderers.Form
{
    /// <summary>
    ///   Simple Button renderer.
    /// </summary>
    [ClassName( "Ext.Button" )]
    internal class Button : Widget
    {
        //protected string _CLICK = "{0}.on(\"click\", function() {{ {0}.disable(); SendToServer(\"{0}\", \"Click\", \"{1}\", null); }}, {0});";
        protected string _CLICK = "NetJsWire.ClickHandler";
        protected string _CLICK_CLIENT_NAME = "click";


        public Button( object baseObject )
            : base( baseObject )
        {
        }

        public override void Render()
        {
            if ( !Rendered )
            {
                if ( BaseParent is IWidgetContainer )
                {
                    SetAttribute( ControlNode, "isInContainer", true );

                    if ( ( ( Ui.Form.Button ) Base ).FormBind )
                        SetConstructionProperty( "formBind", true );
                }
                else
                {
                    SetConstructionProperty( "renderTo", ClientParentToRender );
                    SetStyleProperty( "position", "absolute" );
                }

                EnsureParentNodePosition();
                SetConstructionProperty( "itemId", InternalID );

                SetConstructionProperty( "minWidth", ( ( Ui.Form.Button ) Base ).ClientSize.Width );

                if ( ( ( Ui.Form.Button ) Base ).TabIndex > 0 )
                    SetConstructionProperty( "tabIndex", ( ( Ui.Form.Button ) Base ).TabIndex );

                if ( ( ( Ui.Form.Button ) Base ).Image != null )
                {
                    SetConstructionProperty( "cls", string.IsNullOrEmpty( ( ( Ui.Form.Button ) Base ).Text ) ? "x-btn-icon" : "x-btn-text-icon" );

                    if ( string.IsNullOrEmpty( ( ( Ui.Form.Button ) Base ).IconCls ) )
                        SetConstructionProperty( "icon", "webapp/Images.ashx?_id=" + InternalID );
                }

                if ( !string.IsNullOrEmpty( ( ( Ui.Form.Button ) Base ).IconCls ) )
                {
                    SetConstructionProperty( "iconCls", ( ( Ui.Form.Button ) Base ).IconCls );

                    SetConstructionProperty( "cls", string.IsNullOrEmpty( ( ( Ui.Form.Button ) Base ).Text ) ? "x-btn-icon" : "x-btn-text-icon" );
                }

                if ( !string.IsNullOrEmpty( ( ( Ui.Form.Button ) Base ).IconCls ) || ( ( Ui.Form.Button ) Base ).Image != null )
                {
                    var align = ( ( Ui.Form.Button ) Base ).ImageAlign.ToString().ToLower();
                    var _iconAlign = "left";
                    if ( align.Contains( "top" ) )
                    {
                        _iconAlign = "top";
                    }
                    else if ( align.Contains( "right" ) )
                    {
                        _iconAlign = "right";
                    }
                    else if ( align.Contains( "bottom" ) )
                    {
                        _iconAlign = "bottom";
                    }

                    SetConstructionProperty( "iconAlign", _iconAlign );
                }

                if ( !( ( Ui.Form.Button ) Base ).Enabled )
                    SetConstructionProperty( "disabled", true );

                if ( ( ( Ui.Form.Button ) Base ).IsSubmit && IsChildOfType( typeof( Ui.Form.FormPanel ) ) )
                {
                    SetConstructionProperty( "handler",
                                             string.Format(
                                                 "function(){{ Ext.getCmp('{0}').getForm().submit({{url:'{1}', waitMsg:'{2}', success: function(fp, o){{ eval(o.result.json);}}}});}}",
                                                 GetParentRenderer<Ui.Form.FormPanel>().Renderer.InternalID,
                                                 !string.IsNullOrEmpty( GetParentRenderer<Ui.Form.FormPanel>().UrlSubmit ) ?
                                                 GetParentRenderer<Ui.Form.FormPanel>().UrlSubmit :
                                                 "webapp/" + GetParentRenderer<Ui.Form.FormPanel>().Renderer.InternalID +
                                                 ".ashx", GetParentRenderer<Ui.Form.FormPanel>().WaitMessage ) );
                }

                if ( ( ( Ui.Form.Button ) Base ).IsReset && ( BaseParent as IWidget ).ConfigOptions.Xtype == "form" )
                {
                    SetConstructionProperty( "handler",
                                             string.Format( "function(){{ Ext.getCmp('{0}').getForm().reset();}}",
                                                            ( BaseParent as IWidget ).Renderer.InternalID ) );
                }
            }
            base.Render();
        }
    }
}